package com.java.order.controller;

import com.java.order.service.OrderService;
import com.java.param.CartListParam;
import com.java.param.OrderParam;
import com.java.param.ProductIdRemoveParam;
import com.java.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/save")
    public R save(@RequestBody OrderParam orderParam){

        return orderService.save(orderParam);
    }

    @PostMapping("/list")
    public R list(@RequestBody @Validated CartListParam cartListParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("用户id缺少，无法查询订单！");
        }
        return orderService.list(cartListParam);
    }
    /**
     * 根据商品id检查所有用户购物车是否有该商品
     * @param productIdRemoveParam
     * @return
     */
    @PostMapping("/product/check")
    public R check(@RequestBody @Validated ProductIdRemoveParam productIdRemoveParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("商品id为空，不可删除！");
        }
        return orderService.check(productIdRemoveParam);
    }
}
